#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("无标题-记事本");
    /*文件菜单栏*/
    QObject::connect(ui->NewAction_N, SIGNAL(triggered(bool)), this, SLOT(NewFileSlot()));
    QObject::connect(ui->OpenAction_O, SIGNAL(triggered(bool)), this, SLOT(OpenFileSlot()));
    QObject::connect(ui->SaveAction_S, SIGNAL(triggered(bool)), this, SLOT(SaveFileSlot()));
    QObject::connect(ui->SaveAsAction_A, SIGNAL(triggered(bool)), this, SLOT(SaveAsFileSlot()));
    QObject::connect(ui->ExitAction_X, SIGNAL(triggered(bool)), this, SLOT(close()));
    /*编辑菜单栏*/
    QObject::connect(ui->UndoAction_U, SIGNAL(triggered(bool)), ui->textEdit, SLOT(undo()));
    QObject::connect(ui->Redo_Action_R, SIGNAL(triggered(bool)), ui->textEdit, SLOT(redo()));
    QObject::connect(ui->CutAction_T, SIGNAL(triggered(bool)), ui->textEdit, SLOT(cut()));
    QObject::connect(ui->CopyAction_C, SIGNAL(triggered(bool)), ui->textEdit, SLOT(copy()));
    QObject::connect(ui->PasteAction_P, SIGNAL(triggered(bool)), ui->textEdit, SLOT(paste()));
    QObject::connect(ui->SelectAllAction_A, SIGNAL(triggered(bool)), ui->textEdit, SLOT(selectAll()));
    QObject::connect(ui->DateAction_D, SIGNAL(triggered(bool)), this, SLOT(CurrentDateTimeSlot()));
    /*格式菜单栏*/
    QObject::connect(ui->FontAction_F, SIGNAL(triggered(bool)), this, SLOT(SetFontSlot()));
    QObject::connect(ui->ColorAction_Y, SIGNAL(triggered(bool)), this, SLOT(SetColorSlot()));
    /*帮助菜单栏*/
    QObject::connect(ui->AboutAction_A, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt()));
    QObject::connect(ui->OfficialWebAction, SIGNAL(triggered(bool)), this, SLOT(OfficialWebSlot()));
    QObject::connect(ui->AboutSoftAction, SIGNAL(triggered(bool)), this, SLOT(AboutSoftWareSlot()));

}

void MainWindow::closeEvent(QCloseEvent *event)
{
    int ret;

    if (ui->textEdit->document()->isModified()) {
        QMessageBox msgBox;
        msgBox.setText("文件已修改");
        msgBox.setInformativeText("你想保存你的更改吗？");
        msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Save);
        ret = msgBox.exec();

        switch (ret) {
        case QMessageBox::Save:
            this->SaveFileSlot();
            break;
        case QMessageBox::Discard:
            event->accept();
            break;
        case QMessageBox::Cancel:
            event->ignore();
            break;
        default:
            // should never be reached
            break;
        }
    } else {
        event->accept();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

/*
 新建文件操作处理函数
*/
void MainWindow::NewFileSlot()
{
    if (ui->textEdit->document()->isModified())
        qDebug() << "yes";
    else {
        ui->textEdit->clear();
        this->setWindowTitle("记事本");
    }

}

/*
 打开文件操作处理函数
*/
void MainWindow::OpenFileSlot()
{
    QString FileName;
    QFile *file = new QFile;
    bool ok;
    QTextStream in(file);

    FileName = QFileDialog::getOpenFileName(this, "Open File", QDir::currentPath());
    qDebug() << "文件名：" << FileName;
    file->setFileName(FileName);
    ok = file->open(QIODevice::ReadOnly);

    if (FileName.isEmpty()) {
       QMessageBox::information(this, "Error Message", "请选择文件");
    }
    if (!ok)
        goto errout;


    ui->textEdit->setText(in.readAll());

errout:
    file->close();
    delete file;

}

/*
 文件保存操作处理函数
*/
void MainWindow::SaveFileSlot()
{
    QFile *file;
    bool ok;
    file = new QFile;

    if (SaveFileName.isEmpty())
        this->SaveAsFileSlot();

    file->setFileName(SaveFileName);
    ok = file->open(QIODevice::WriteOnly);
    if (!ok) {
        QMessageBox::information(this, "Error Message", "文件保存错误");
        return;
    }

    QTextStream out(file);
    out << ui->textEdit->toPlainText(); //去除textEdit中的纯文本
    file->close();
    delete file;

}

/*
 文件另存为操作处理函数
*/
void MainWindow::SaveAsFileSlot()
{
    QFile *file;
    bool ok;

    SaveFileName = QFileDialog::getSaveFileName(this, "Save File", QDir::currentPath());
    if (SaveFileName.isEmpty()) {
        QMessageBox::information(this, "Error Message", "请选择文件");
        return;
    }

    file = new QFile;
    file->setFileName(SaveFileName);
    ok = file->open(QIODevice::WriteOnly);
    if (!ok) {
        QMessageBox::information(this, "Error Message", "文件保存错误");
        return;
    }

    QTextStream out(file);
    out << ui->textEdit->toPlainText(); //去除textEdit中的纯文本
    file->close();
    this->setWindowTitle(SaveFileName+"另存为");
    delete file;

}

void MainWindow::PrintfFileSlot()
{

}

/*
 设置文件字体
*/
void MainWindow::SetFontSlot()
{
    bool ok;

    QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
    if (ok) {
        ui->textEdit->setFont(font);
    } else {
        QMessageBox::information(this, "Error Message", "设置字体失败");
        return;
    }


}

/*
 设置字体颜色
*/
void MainWindow::SetColorSlot()
{
    QColor color;

    color = QColorDialog::getColor(Qt::red, this);
    if (color.isValid()) {
        ui->textEdit->setTextColor(color);
    }
    else {
        QMessageBox::information(this, "Error Message", "设置字体颜色失败");
        return;
    }

}

void MainWindow::CurrentDateTimeSlot()
{
    QDateTime current;
    QString time;

    current = QDateTime::currentDateTime();
    time = current.toString("yyyy-MM-dd hh:mm:ss");
    ui->textEdit->append(time);
}

void MainWindow::OfficialWebSlot()
{
    QDesktopServices::openUrl(QUrl("https://gitee.com/"));
}

void MainWindow::AboutSoftWareSlot()
{
    aboutsoft *dialog = new aboutsoft;
    dialog->show();
}
